<%@ page import="java.util.regex.Matcher" %>
<%@ page import="java.util.regex.Pattern" %>
<%@ page import="javax.naming.Context" %>
<%@ page import="javax.naming.InitialContext" %>
<%@ page import="edu.columbia.ubms.ejb.UserInfoSessionBeanRemote" %>

<%
String userName = request.getParameter("username");

if (userName.length() < 1 || userName.length() > 16) {
	 response.setContentType("text/xml");
	 response.setHeader("Cache-Control", "no-cache");
	 response.getWriter().write("You can not use this username.\nUsername should be any alphanumeric string with 16 characters or less.");
} else {
	Pattern pattern;
	Matcher matcher;
	 
	pattern = 
	       Pattern.compile("[^0-9a-zA-Z()-_]");
	
	matcher = 
	       pattern.matcher(userName);

	if(matcher.find()) 
	{
		 response.setContentType("text/xml");
		 response.setHeader("Cache-Control", "no-cache");
		 response.getWriter().write("You can not use this username.\nUsername should be any alphanumeric string with 16 characters or less.");
	} else {
		try {   
		    Context ctx = new InitialContext();		
			UserInfoSessionBeanRemote beanRemote = (UserInfoSessionBeanRemote)ctx.lookup("UserInfoSessionBean/remote");
			boolean userExists = beanRemote.userDupCheck(userName);
			
			if (userExists) {
				 response.setContentType("text/xml");
				 response.setHeader("Cache-Control", "no-cache");
				 response.getWriter().write("You can not use this username.\nThe username is duplicated.");
			} else {
				 response.setContentType("text/xml");
				 response.setHeader("Cache-Control", "no-cache");
				 response.getWriter().write("You can use this username.");
			}
		} catch(Exception e) {
		    out.println("Error:" + e.getMessage());
		}
	}
}
%>